package xyz.lovegu.server.sys.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import xyz.lovegu.server.sys.common.DataGridView;
import xyz.lovegu.server.sys.common.ResultObj;
import xyz.lovegu.server.sys.dos.Classes;
import xyz.lovegu.server.sys.dos.Notice;
import xyz.lovegu.server.sys.mapper.ClassesMapper;
import xyz.lovegu.server.sys.service.ClassesService;
import xyz.lovegu.server.sys.vos.ClassesVo;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Date;

/**
 * @author 老顾
 * @title: ClassesServiceImpl
 * @projectName campus-prevention-system
 * @email: 1437594522@qq.com
 * @date 2022/5/7 23:44
 */
@Service
public class ClassesServiceImpl extends ServiceImpl<ClassesMapper, Classes> implements ClassesService {

    /**
     * 增加班级
     *
     * @param classesVo
     * @return
     */
    @Override
    public ResultObj addClasses(ClassesVo classesVo) {
        try {
            Classes classes = new Classes();
            BeanUtils.copyProperties(classesVo,classes);
            this.save(classes);
            return ResultObj.ADD_SUCCESS;
        }catch (Exception e){
            e.printStackTrace();
            return ResultObj.ADD_ERROR;
        }
    }

    /**
     * 根据id删除班级（逻辑删除）
     *
     * @param classesVo
     * @return
     */
    @Override
    public ResultObj deleteClassesById(ClassesVo classesVo) {
        //查询班级是否存在
        Classes classes = this.getById(classesVo.getId());
        if (classes==null){
            return ResultObj.CLASS_NOT_EXIST;
        }
        try {
            this.removeById(classesVo.getId());
            return ResultObj.DELETE_SUCCESS;
        }catch (Exception e){
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }

    /**
     * 批量删除班级
     *
     * @param classesVo
     * @return
     */
    @Override
    public ResultObj deleteBatchClasses(ClassesVo classesVo) {
        try {
            Collection<Serializable> list = new ArrayList<>(Arrays.asList(classesVo.getIds()));
            this.removeByIds(list);
            return ResultObj.DELETE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }

    /**
     * 修改班级
     *
     * @param classesVo
     * @return
     */
    @Override
    public ResultObj updateClasses(ClassesVo classesVo) {
        //查询班级是否存在
        Classes classes = this.getById(classesVo.getId());
        if (classes==null){
            return ResultObj.CLASS_NOT_EXIST;
        }
        try {
            classes.setClassId(classesVo.getClassId());
            classes.setClassName(classesVo.getClassName());
            classes.setUpdateTime(new Date());
            this.updateById(classes);
            return ResultObj.UPDATE_SUCCESS;
        }catch (Exception e){
            e.printStackTrace();
            return ResultObj.UPDATE_ERROR;
        }
    }

    /**
     * 根据id查询单个班级
     *
     * @param id
     * @return
     */
    @Override
    public Object getClassesById(Integer id) {
        Classes classes = this.getById(id);
        if (classes == null) {
            return ResultObj.CLASS_NOT_EXIST;
        } else {
            return new DataGridView(classes);
        }
    }

    /**
     * 查询所有班级
     *
     * @param classesVo
     * @return
     */
    @Override
    public Object getAllClasses(ClassesVo classesVo) {
        IPage<Classes> page = new Page<Classes>(classesVo.getPage(),classesVo.getLimit());
        LambdaQueryWrapper<Classes> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //进行模糊查询
        lambdaQueryWrapper.like(StrUtil.isNotBlank(classesVo.getClassName()),Classes::getClassName,classesVo.getClassName());
        //班级创建时间应大于搜索开始时间小于结束时间
        lambdaQueryWrapper.eq(classesVo.getStartTime()!=null,Classes::getCreateTime,classesVo.getStartTime());
        lambdaQueryWrapper.eq(classesVo.getEndTime()!=null,Classes::getCreateTime,classesVo.getEndTime());
        //根据创建时间排序
        lambdaQueryWrapper.orderByDesc(Classes::getCreateTime);
        this.page(page,lambdaQueryWrapper);
        return new DataGridView(page.getTotal(),page.getRecords());
    }
}
